package com.loto.insurance.oem.web.controller.ajax;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.oem.web.entity.CustExtInfo;
import com.loto.insurance.oem.web.entity.CustInfo;
import com.loto.insurance.oem.web.entity.SessionUser;
import com.loto.insurance.oem.web.authority.OEMAuthority;
import com.loto.insurance.oem.web.entity.ajax.BaseJson;
import com.loto.insurance.oem.web.enumeration.AuthorityRoleEnum;
import com.loto.insurance.oem.web.enumeration.ControllerPropertyEnum;
import com.loto.insurance.oem.web.service.CustInfoService;
import com.loto.utils.MD5;

@Controller
public class CustInfoController {

  @Autowired
  private CustInfoService custInfoService;

  @RequestMapping(value = "/login.json", method = RequestMethod.POST)
  public ModelAndView doLogin(HttpServletRequest request,
	  @RequestParam(value = "name", required = false) String loginName,
	  @RequestParam(value = "pwd", required = false) String loginPwd) {
	ModelAndView mav = new ModelAndView();
	// List<GoodsInfo> goodsList = goodsInfoService.findUnPurchased(67001);
	BaseJson base = new BaseJson();
	// 判断验证码

	// 验证用户名和密码
	base = custInfoService.doLogin(request, loginName, loginPwd);
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority
  @RequestMapping(value = "/changepwd.json", method = RequestMethod.POST)
  public ModelAndView changePwd(HttpServletRequest request,
	  @RequestParam(value = "oldPwd", required = true) String oldPwd,
	  @RequestParam(value = "pwd", required = true) String pwd) {
	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	HttpSession session = request.getSession();
	SessionUser user = (SessionUser) session.getAttribute("oem_user");
	if (user != null) {
	  base = custInfoService.changePwd(user.getUsername(), oldPwd, pwd);
	} else {
	  base.setOk(false);
	  base.setMsg(ControllerPropertyEnum.NOT_LOGIN.getName());
	}
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.ADMIN)
  @RequestMapping(value = "/oem/usermodify.json", method = RequestMethod.POST)
  public ModelAndView custModify(
	  @RequestParam(value = "id", required = true) long custId,
	  @RequestParam(value = "pwd", required = false) String custPwd,
	  @RequestParam(value = "email", required = true) String custEmail,
	  @RequestParam(value = "level", required = true) int custLevelFid,
	  @RequestParam(value = "usefulFee", required = true) BigDecimal custUsefulFee,
	  @RequestParam(value = "status", required = true) int custStatus,
	  @RequestParam(value = "userFlg", required = true) int custUserFlg,
	  @RequestParam(value = "contactName", required = true) String extContactName,
	  @RequestParam(value = "contactTel", required = true) String extContactTel,
	  @RequestParam(value = "contactOthers", required = false) String extContactOthers,
	  @RequestParam(value = "companyName", required = false) String extCompanyName,
	  @RequestParam(value = "companyZip", required = false) String extCompanyZip,
	  @RequestParam(value = "companyAddress", required = false) String extCompanyAddress,
	  @RequestParam(value = "companyTel", required = false) String extCompanyTel,
	  @RequestParam(value = "companyFax", required = false) String extCompanyFax,
	  @RequestParam(value = "companyEmail", required = false) String extCompanyEmail,
	  @RequestParam(value = "companyWebsite", required = false) String extCompanyWebsite,
	  @RequestParam(value = "companyReg", required = false) String extCompanyReg,
	  @RequestParam(value = "chargeName", required = false) String extChargeName,
	  @RequestParam(value = "from", required = false) String extFrom) {

	CustInfo custInfo = new CustInfo();
	custInfo.setCustEmail(custEmail);
	custInfo.setCustId(custId);
	custInfo.setCustLevelFid(custLevelFid);
	if (custPwd != null && !custPwd.isEmpty())
	  custInfo.setCustPwd(MD5.GetMD5Code(custPwd));
	custInfo.setCustStatus(custStatus);
	custInfo.setCustUsefulFee(custUsefulFee);
	custInfo.setCustUserFlg(custUserFlg);

	CustExtInfo custExtInfo = new CustExtInfo();
	custExtInfo.setExtChargeName(extChargeName);
	custExtInfo.setExtCompanyAddress(extCompanyAddress);
	custExtInfo.setExtCompanyEmail(extCompanyEmail);
	custExtInfo.setExtCompanyFax(extCompanyFax);
	custExtInfo.setExtCompanyName(extCompanyName);
	custExtInfo.setExtCompanyReg(extCompanyReg);
	custExtInfo.setExtCompanyTel(extCompanyTel);
	custExtInfo.setExtCompanyWebsite(extCompanyWebsite);
	custExtInfo.setExtCompanyZip(extCompanyZip);
	custExtInfo.setExtContactName(extContactName);
	custExtInfo.setExtContactOthers(extContactOthers);
	custExtInfo.setExtContactTel(extContactTel);
	custExtInfo.setExtCustFid(custId);
	custExtInfo.setExtFrom(extFrom);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = custInfoService.updateCustInfo(custInfo, custExtInfo);
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.ADMIN)
  @RequestMapping(value = "/oem/levelmodify.json", method = RequestMethod.POST)
  public ModelAndView levelModify(
	  @RequestParam(value = "id", required = true) int levelId,
	  @RequestParam(value = "name", required = false) String levelName,
	  @RequestParam(value = "discount", required = true) BigDecimal levelDiscount) {
	BaseJson base = new BaseJson();
	base.setOk(false);
	if (levelDiscount.compareTo(new BigDecimal(1)) < 0) {
	  base.setOk(false);
	  base.setMsg(ControllerPropertyEnum.WRONG_DISCOUNT.getName());
	} else {
	  int result = 0;
	  if (custInfoService.levelIsFixed(levelId)) {
		result = custInfoService.updateLevel(levelId, levelDiscount);
	  } else {
		result = custInfoService.updateLevel(levelId, levelName, levelDiscount);
	  }
	  if (result > 0) {
		base.setOk(true);
	  } else {
		base.setMsg(ControllerPropertyEnum.DATA_WRONG.getName());
	  }
	}
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.ADMIN)
  @RequestMapping(value = "/oem/leveladd.json", method = RequestMethod.POST)
  public ModelAndView levelAdd(
	  @RequestParam(value = "name", required = true) String levelName,
	  @RequestParam(value = "discount", required = true) BigDecimal levelDiscount) {
	BaseJson base = new BaseJson();
	base.setOk(false);
	if (levelDiscount.compareTo(new BigDecimal(1)) < 0) {
	  base.setOk(false);
	  base.setMsg(ControllerPropertyEnum.WRONG_DISCOUNT.getName());
	} else {
	  if (custInfoService.insertLevel(levelName, levelDiscount) > 0) {
		base.setOk(true);
	  } else {
		base.setMsg(ControllerPropertyEnum.LEVEL_EXISTED.getName());
	  }
	}
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", base);
	return mav;
  }

  @RequestMapping(value = "/reg/checkname.json", method = RequestMethod.GET)
  public ModelAndView regcheckname(
	  @RequestParam(value = "userName", required = true) String custName) {
	BaseJson base = new BaseJson();
	base.setOk(false);
	if (custInfoService.isCustExisted(custName))
	  base.setOk(false);
	else
	  base.setOk(true);
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", base);
	return mav;
  }

  @RequestMapping(value = "/reg/checkemail.json", method = RequestMethod.GET)
  public ModelAndView regcheckemail(
	  @RequestParam(value = "email", required = true) String email) {
	BaseJson base = new BaseJson();
	base.setOk(false);
	if (custInfoService.isEmailExisted(email))
	  base.setOk(false);
	else
	  base.setOk(true);
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", base);
	return mav;
  }

  @RequestMapping(value = "/reg/useradd.json", method = RequestMethod.POST)
  public ModelAndView custadd(
	  @RequestParam(value = "name", required = true) String custName,
	  @RequestParam(value = "pwd", required = true) String custPwd,
	  @RequestParam(value = "email", required = true) String custEmail,
	  @RequestParam(value = "contactName", required = true) String extContactName,
	  @RequestParam(value = "contactTel", required = true) String extContactTel,
	  @RequestParam(value = "contactOthers", required = true) String extContactOthers,
	  @RequestParam(value = "companyName", required = true) String extCompanyName,
	  @RequestParam(value = "companyZip", required = true) String extCompanyZip,
	  @RequestParam(value = "companyAddress", required = true) String extCompanyAddress,
	  @RequestParam(value = "companyTel", required = true) String extCompanyTel,
	  @RequestParam(value = "companyWebsite", required = true) String extCompanyWebsite,
	  @RequestParam(value = "companyReg", required = true) String extCompanyReg,
	  @RequestParam(value = "chargeName", required = true) String extChargeName,
	  @RequestParam(value = "from", required = true) String extFrom) {

	CustInfo custInfo = new CustInfo();
	custInfo.setCustEmail(custEmail);
	custInfo.setCustName(custName);
	custInfo.setCustLevelFid(0);
	custInfo.setCustPwd(MD5.GetMD5Code(custPwd));
	custInfo.setCustStatus(1);
	custInfo.setCustUsefulFee(new BigDecimal(0));
	custInfo.setCustUserFlg(0);

	CustExtInfo custExtInfo = new CustExtInfo();
	custExtInfo.setExtChargeName(extChargeName);
	custExtInfo.setExtCompanyAddress(extCompanyAddress);
	// custExtInfo.setExtCompanyEmail(extCompanyEmail);
	// custExtInfo.setExtCompanyFax(extCompanyFax);
	custExtInfo.setExtCompanyName(extCompanyName);
	custExtInfo.setExtCompanyReg(extCompanyReg);
	custExtInfo.setExtCompanyTel(extCompanyTel);
	custExtInfo.setExtCompanyWebsite(extCompanyWebsite);
	custExtInfo.setExtCompanyZip(extCompanyZip);
	custExtInfo.setExtContactName(extContactName);
	custExtInfo.setExtContactOthers(extContactOthers);
	custExtInfo.setExtContactTel(extContactTel);

	custExtInfo.setExtFrom(extFrom);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = custInfoService.addCustInfo(custInfo, custExtInfo);
	mav.addObject("result", base);
	return mav;
  }
}
